// 封装请求：get post
// 面向对象ES6 promisr 等等

// 引入提示
import {errdata} from './errdata.js';

// Get 请求
const getRequest = function(urling) {
	// 处理请求携带参数  uni.getStorageSync('token') 同步获取缓存数据
	let token = uni.getStorageSync('token')
	let headers = {
		"accept": "*/*",
		"Content-Type": "application/json",
	};
	
	// 将token放入到请求头当中
	headers['token'] = token;
	
	return new Promise((resolve, reject)=> {
		uni.request({
			url: urling,
			method: 'GET',
			header: headers
		})
		.then((res)=> {
			// 返回的结果第二个是真正的结果
			console.log(res);
			if(res[1].data.code === 20000 || res[1].data.code === 200) {
				resolve(res[1]);
			} else {
				let mes = res[1].data.message;
				errdata.errlist(mes);
				reject(res[1]);
			}
		})
		.catch((err)=> {
			let errInfo = '服务器错误，请稍后再试';
			errdata.errlist(errInfo);
			reject(err);
		})
	})
}


// post  请求
const getPost = function(urling, shopdata) {
	// 处理请求携带参数  
	let token = uni.getStorageSync('token')
	let headers = {
		"accept": "*/*",
		"Content-Type": "application/json"
	};
	
	// 将token放入到请求头当中
	headers['token'] = token;
	
	console.log("-------------------")
	console.log(urling);
	console.log(headers);
	console.log(shopdata);
	console.log("-------------------")
	
	return new Promise((resolve, reject)=> {
		uni.request({
			url: urling,
			method: 'POST',
			data: shopdata,
			header: headers
		})
		.then((res)=> {
			console.log(res);
			if(res[1].data.code === 20000 || res[1].data.code === 200) {
				resolve(res[1]);
			} else {
				let mes = res[1].data.message;
				errdata.errlist(mes);
				reject(res[1]);
			}
			
			
		})
		.catch((err)=> {
			// 返回的结果第二个是真正的结果
			let errInfo = '服务器错误，请稍后再试';
			errdata.errlist(errInfo);
			reject(err);
		})
	})
}

// 将其导出
export {getRequest, getPost}
